package io.github.cubelitblade.librarymanagementbackend.controller;

import io.github.cubelitblade.librarymanagementbackend.entity.Book;
import io.github.cubelitblade.librarymanagementbackend.service.BookService;
import io.github.cubelitblade.librarymanagementbackend.utils.ResultContext;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/books")
public class BookController {
    // 构造器注入。
    private final BookService bookService;
    BookController(BookService bookService) {
        this.bookService = bookService;
    }

    @GetMapping("/recommendations/random")
    public ResponseEntity<ResultContext<List<Book>>> getRandomRecommendations(@RequestParam(defaultValue = "8") int count) {
        return ResponseEntity.ok(ResultContext.succeed(bookService.getRandomBookList(count)));
    }

    @GetMapping("/trending/borrowing")
    public ResponseEntity<ResultContext<List<Book>>> getTopBorrowedBooks(@RequestParam(defaultValue = "10") int count) {
        return ResponseEntity.ok(ResultContext.succeed(bookService.getBooksByBorrowing(count)));
    }

    @GetMapping("/trending/favorites")
    public ResponseEntity<ResultContext<List<Book>>> getTopFavoriteBooks(@RequestParam(defaultValue = "10") int count) {
        return ResponseEntity.ok(ResultContext.succeed(bookService.getBooksByFavorite(count)));
    }

    @GetMapping("/{bookId}")
    public ResponseEntity<ResultContext<Book>> getBookDetail(@PathVariable Integer bookId) {
        return ResponseEntity.ok(ResultContext.succeed(bookService.getById(bookId)));
    }

    // 搜索结果页接口
    @GetMapping("/search")
    public ResponseEntity<ResultContext<List<Book>>> searchBooks(@RequestParam String keyword) {
        List<Book> result = bookService.searchBooks(keyword);
        return ResponseEntity.ok(ResultContext.succeed(result));
    }

    // 搜索建议接口
    @GetMapping("/suggestions")
    public ResponseEntity<ResultContext<List<Map<String, Object>>>> getSuggestionsByKeyword(@RequestParam String keyword) {
        List<Map<String, Object>> result = bookService.suggestBooks(keyword, 5);
        return ResponseEntity.ok(ResultContext.succeed(result));
    }
}
